package games;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class MyFrame extends JFrame implements KeyListener,ActionListener {

    private int [][]arr=new int[][]{{1,2,3},
                                    {4,5,6},
                                    {7,8,9}
                                    };
    private final int [][]correctArr={{1,2,3},
                                 {4,5,6},
                                   {7,8,9}
    };
    private int count=0;
    private int row;
    private int column;
    public MyFrame(){
        mixPicture();
        mainFrame();
        paintView();
        setVisible(true);
    }

    public void  mainFrame(){
        this.addKeyListener(this);
//        MyFrame myFrame=new MyFrame();
//        myFrame.addKeyListener(myFrame);
        setSize(1000,1000);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
        setTitle("拼图1.0");
        setAlwaysOnTop(true);


    }
    public void paintView(){
        /**    没有优化的代码
         JLabel lable=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\1.jpg"));
         lable.setBounds(50,50,300,300);
         frame.getContentPane().add(lable);

         JLabel lable2=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\2.jpg"));
         lable2.setBounds(350,50,300,300);
         frame.getContentPane().add(lable2);

         JLabel lable3=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\3.jpg"));
         lable3.setBounds(650,50,300,300);
         frame.getContentPane().add(lable3);

         JLabel lable4=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\4.jpg"));
         lable4.setBounds(50,350,300,300);
         frame.getContentPane().add(lable4);

         JLabel lable5=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\5.jpg"));
         lable5.setBounds(350,350,300,300);
         frame.getContentPane().add(lable5);

         JLabel lable6=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\6.jpg"));
         lable6.setBounds(650,350,300,300);
         frame.getContentPane().add(lable6);

         JLabel lable7=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\7.jpg"));
         lable7.setBounds(50,650,300,300);
         frame.getContentPane().add(lable7);

         JLabel lable8=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\8.jpg"));
         lable8.setBounds(350,650,300,300);
         frame.getContentPane().add(lable8);

         JLabel lable9=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\9.jpg"));
         lable9.setBounds(650,650,300,300);
         frame.getContentPane().add(lable9);

         */
        //清空窗口面板
        getContentPane().removeAll();
            if (victory()){
                JLabel victoryLable=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\victory.jpg"));
                victoryLable.setBounds(315,400,371,300);
                getContentPane().add(victoryLable);
            }

        for (int i = 0; i <3 ; i++) {
            for (int j = 0; j <3 ; j++) {
                JLabel lable=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\"+arr[i][j]+".jpg"));
                lable.setBounds(50+j*300,50+i*300,300,300);
                getContentPane().add(lable);
            }
        }

        JButton replay=new JButton("重新开始");
        replay.setFocusable(false);
        replay.setBounds(750,30,100,20);
        getContentPane().add(replay);
//        replay.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                count=0;
//                mixPicture();
//                paintView();
//            }
//        });
        replay.addActionListener(this);

        JLabel countLable=new JLabel("步数:"+count);
        countLable.setBounds(50,30,100,20);
        getContentPane().add(countLable);

        JLabel lable10=new JLabel(new ImageIcon("D:\\java\\JavaMaterial\\picture\\girls.jpg"));
        lable10.setBounds(0,50,1000,900);
        getContentPane().add(lable10);
        //刷新面板
        getContentPane().repaint();
    }

    public void mixPicture() {
        Random random=new Random();
        for (int i = 0; i <3 ; i++) {
            for (int j = 0; j <3 ; j++) {
                int x=random.nextInt(3);
                int y=random.nextInt(3);
                int temp=arr[i][j];
                arr[i][j]=arr[x][y];
                arr[x][y]=temp;
            }
        }
        for (int i = 0; i <arr.length ; i++) {
            for (int j = 0; j <arr.length ; j++) {
                if (arr[i][j]==9){
                    row=i;
                    column=j;
                }

            }
        }
//        System.out.println(row+" "+column);
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }
    public void setCount(int count){
        this.count=count;
    }
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode=e.getKeyCode();
        movePicture(keyCode);
        paintView();
    }

    private boolean victory() {
        boolean victory=true;

        for (int i = 0; i <correctArr.length ; i++) {
            for (int j = 0; j <correctArr.length ; j++) {
                if (correctArr[i][j]!=arr[i][j]){
                    victory=false;
                }
            }
        }

        return victory;
    }

    private void movePicture(int keyCode) {
        if (victory()){
            return;
        }
        if (keyCode ==65||keyCode ==97){
            if (column!=2){
                int temp=arr[row][column];
                arr[row][column]=arr[row][column+1];
                arr[row][column+1]=temp;
                column++;
                count++;
        }

        }else if (keyCode ==87||keyCode ==119){
            if (row!=2){
                int temp=arr[row][column];
                arr[row][column]=arr[row+1][column];
                arr[row+1][column]=temp;
                row++;
                count++;
            }
        }else if (keyCode ==68||keyCode ==100){
            if (column!=0){
                int temp=arr[row][column];
                arr[row][column]=arr[row][column-1];
                arr[row][column-1]=temp;
                column--;
                count++;
            }

        }else if (keyCode ==83||keyCode ==115){
            if (row!=0){
                int temp=arr[row][column];
                arr[row][column]=arr[row-1][column];
                arr[row-1][column]=temp;
                row--;
                count++;
            }
        }else if (keyCode ==32){
            arr=new int[][]{          {1,2,3},
                                      {4,5,6},
                                      {7,8,9}
            };
            row=2;
            column=2;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        count=0;
        mixPicture();
        this.paintView();
    }
}
